define(function () {
    'use strict';
    function ctrl($scope, $state, $ionicLoading, LocalCacheService, CommonService, IdentificationService, TdxLocalFuncService) {
        function dateFormater(input) {
            var show;
            if (input.length == 8) {
                show = input.substr(0, 4) + "-" + input.substr(4, 2) + "-" + input.substr(6, 2);
            } else {
                show = input.substr(0, 4) + "-" + input.substr(4, 2) + "-" + input.substr(6, 2) + " "
                    + input.substr(8, 2) + ":" + input.substr(10, 2) + ":" + input.substr(12, 2);
            }
            return show;
        }

        function init() {
            $scope.idStatus = {};
            $ionicLoading.show();
            IdentificationService.init()
                .then(function (data) {
                    if (data.error_no != 0) {
                        $ionicLoading.hide();
                        CommonService.showAlert({message: data.error_info});
                        return;
                    }
                    IdentificationService.getRecordStatus()
                        .then(function (data) {
                            $ionicLoading.hide();
                            if (data.error_no != 0) {
                                CommonService.showAlert({message: data.error_info});
                                return;
                            }
                            if (data.idcard_term_record_status == 100) {
                                $scope.idStatus.status = 1;
                                $scope.idStatus.subtext = '您的身份证有效期信息异常，无法进行该项操作！';
                                return;
                            }
                            /* else if (data.idcard_term_record_status == 2 && data.idcard_term_record_url6a && data.idcard_term_record_url6b) {
                             $state.go('tab.account-userInfoVideo');
                             return;
                             }*/
                            $scope.idStatus.status = 0;
                            var user = LocalCacheService.getUser();
                            var idDate = dateFormater(user.id_begindate) + ' ~ ' + dateFormater(user.id_enddate);
                            $scope.identifications = [{key: '姓名', value: user.client_name},
                                {key: '身份证', value: user.id_no},
                                {key: '证件地址', value: user.id_address},
                                {key: '签发机关', value: user.issued_depart},
                                {key: '证件有效期', value: idDate}];
                        }, function (result) {
                            $ionicLoading.hide();
                            CommonService.showAlert({message: result.error_info});
                        });
                }, function (result) {
                    $ionicLoading.hide();
                    CommonService.showAlert({message: result.error_info});
                    $state.go('tab.account-manager');
                })
        }

        $scope.change = function () {
            TdxLocalFuncService.tdxTradeLocked().then(function (data) {
                if (data.text == '1' || data.text==1) {
                    return;
                } else {
                    var currentTime = new Date();
                    var hour = currentTime.getHours();
                    var day = currentTime.getDay();
                    if ((day == 0 || day == 6) || (hour < 8 || hour > 22)) {
                        CommonService.showConfig({message: '请在交易日08:00--22:00内办理。'});
                    } else {
                        $state.go('tab.account-changeid');
                    }
                }
            })
        };
        $scope.$on('$ionicView.beforeEnter', function () {
            init();
        });
    }

    ctrl.$inject = ['$scope', '$state', '$ionicLoading', 'LocalCacheService', 'CommonService', 'IdentificationService', 'TdxLocalFuncService'];
    return ctrl;
});